package cn.edu.niit.alienlab.jshttplessonserver.apis;

import cn.edu.niit.alienlab.jshttplessonserver.dto.HeroInfo;
import cn.edu.niit.alienlab.jshttplessonserver.service.Lesson2DataTool;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class Lesson2Resource {
    @GetMapping("/heros")
    public ResponseEntity getHeros(){
        List<HeroInfo> result= Lesson2DataTool.getData();
        return ResponseEntity.ok(result);
    }

    @GetMapping("/heros/mine")
    public ResponseEntity getMyHeros(){
        List<HeroInfo> heros= Lesson2DataTool.getData();
        Random random=new Random();
        List<HeroInfo> result=new ArrayList<>();
        for(int i=0;i<6;i++){
            int n=random.nextInt(heros.size()-1);
            result.add(heros.get(n));
        }
        return ResponseEntity.ok(result);
    }
}
